/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.view.template;

import java.awt.Dialog;
import java.net.URL;
import java.util.HashMap;
import javax.swing.JDialog;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.veditor.model.messages.TemplateEditorResult;
import offset.nodes.client.editor.model.EditorDocument;
import offset.nodes.client.veditor.VirtualEditor;
import offset.nodes.client.view.ResultState;

/**
 *
 * @author Walter Lütgenau
 */
public class EditorDialog extends JDialog {
    Editor editor;

    public EditorDialog(Dialog dialog, boolean modal,
            String html, NodeTypes types, URL server, URL repository,
            HashMap<String,String> secondaryQueries, String templateNodePath) throws Exception {
        super(dialog, modal);
  
        editor = new Editor();
        editor.init();
        if ( html != null) {
            ((EditorDocument)editor.getBrowserPane().getDocument()).setBase(repository);
            editor.getBrowserPane().setText(html);
            editor.setMode(VirtualEditor.MODE_TEMPLATE_EDITOR);
        }
        editor.setContainer(this);
        editor.getProperties().put(Editor.PROP_TEMPLATE_NODE_PATH, templateNodePath);
        editor.getProperties().put(Editor.PROP_DOCUMENT_URL, repository.toString());
        if ( secondaryQueries != null)
            editor.getProperties().put(Editor.PROP_SECONDARY_QUERIES, secondaryQueries);
        else
            editor.getProperties().put(Editor.PROP_SECONDARY_QUERIES, new HashMap<String,String>());
        editor.getProperties().put(Editor.PROP_NODE_TYPES, types);
        editor.getProperties().put(Editor.PROP_UPLOAD_TO, server.toString());
        editor.start();

        add(editor);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("nodes");
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/editor.png")).getImage());
        setLocationByPlatform(true);

        pack();
        setVisible(true);
    }
    
    public ResultState getReturnCode() {
        return editor.getResult().getState();
    }
    
    public TemplateEditorResult getResult() {
        return (TemplateEditorResult) editor.getResult().getValue();
    }
}
